//
//  AnswerTableViewCell.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/6/20.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
protocol AnswerTableViewCellDelegate {
    func shareBtnSelected(id: String)
    func zanBtnSelected(id: String)
    func commentBtnSelected(id: String, docid: String)
    
}
class AnswerTableViewCell: UITableViewCell {
    
    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var context: UILabel!
    @IBOutlet weak var originalText: UILabel!

    @IBOutlet weak var zanBtn: UIButton!
    @IBOutlet weak var commentBtn: UIButton!
    
   
    var zanBtnClicked: (() -> Void)?
    var conmenBtnClicked: (() -> Void)?
    
    var delegate: AnswerTableViewCellDelegate?
    var model: AnswerModel? {
        didSet {
            self.setupView()
        }
    }
    var cellHeight: CGFloat!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setCellContentWithCellInfo(text: String, andCommenText: String) -> Void {
        let font = UIFont(name: "Helvetica", size: 16)
        var height = publicMethod.getLabHeigh(text, font:font!, width:SCREEN_WIDTH - 16)
        height += publicMethod.getLabHeigh(andCommenText, font:font!, width:SCREEN_WIDTH - 16)
        cellHeight = height + 120
    }
   
    @IBAction func zanBtnClicked(sender: UIButton){

        if self.model?.isdianzan == true {
            SVProgressHUD.showErrorWithStatus("您已经赞过")
        }
        DataTool.supposeComment(Int((self.model?.id)!)!, type: "行业" , completionHandler: { (isSuccess) -> Void in
            if isSuccess {
               
                self.zanBtn.setBackgroundImage(UIImage(named: "zan_selected"), forState: UIControlState.Normal)
            } else {
                SVProgressHUD.showErrorWithStatus("点赞失败")
                
            }
        })

        //delegate?.zanBtnSelected(String(self.model!.id))
        
    }
    @IBAction func commentBtnClicked(sedner: UIButton){
        //delegate?.commentBtnSelected(String(self.model!.id), docid: String(self.model!.docid))
        if self.conmenBtnClicked != nil {
            self.conmenBtnClicked!()
        }
        
    }
    
    func setupView() {
        //icon.sd_setImageWithURL(NSURL(string: model!.timg))
        //icon.sd_setImageWithURL(NSURL(string: (model?.timg)!), placeholderImage: UIImage(named: "placeholder"))
        icon.sd_setImageWithURL(NSURL(string: (model?.timg)!), placeholderImage: UIImage(named: "placeholder"))
        name.text = model?.name
        time.text = model?.commenttime
        context.text = model?.message
        originalText.text = model?.comment?.message
    }
}
